package aerouacmsla.interfazDeUsuario;

import aerouacmsla.entidad.Avion;
import aerouacmsla.procedimientos.BDConexion;
import aerouacmsla.procedimientos.CreateTables;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Marco
 */
public class PanelAdministrarAviones extends javax.swing.JPanel {

    private BDConexion miConexion = null;
    private CreateTables modeloTabla = new CreateTables();

    public PanelAdministrarAviones() {
        initComponents();
    }

    public PanelAdministrarAviones(BDConexion miConexion) {
        initComponents();
        setMiConexion(miConexion);
        jTable4.setModel(getModeloTabla().getTableModel(getMiConexion().sendSQLQuery("SELECT * FROM avion")));
    }

    public void setTabla(String SELECT){
        jTable4.setModel(getModeloTabla().getTableModel(getMiConexion().sendSQLQuery(SELECT)));
    }

    public BDConexion getMiConexion() {
        return miConexion;
    }

    public void setMiConexion(BDConexion miConexion) {
        this.miConexion = miConexion;
    }

    public CreateTables getModeloTabla() {
        return modeloTabla;
    }

    public void setModeloTabla(CreateTables modeloTabla) {
        this.modeloTabla = modeloTabla;
    }

    public javax.swing.JPanel getPanelAviones(){
        return this;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        panelTabla3 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable4 = new org.jdesktop.swingx.JXTable();
        panelBotones = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        textFieldBusqueda = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        radioModelo = new javax.swing.JRadioButton();
        radioMarca = new javax.swing.JRadioButton();
        radioEstado = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();

        jTable4.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable4.getTableHeader().setReorderingAllowed(false);
        jScrollPane4.setViewportView(jTable4);

        javax.swing.GroupLayout panelTabla3Layout = new javax.swing.GroupLayout(panelTabla3);
        panelTabla3.setLayout(panelTabla3Layout);
        panelTabla3Layout.setHorizontalGroup(
            panelTabla3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
        );
        panelTabla3Layout.setVerticalGroup(
            panelTabla3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
        );

        panelBotones.setBorder(javax.swing.BorderFactory.createTitledBorder("Funciones"));

        jButton1.setText("Nuevo Avión");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("Modificar Avión");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jButton3.setText("Buscar Avión");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton4.setText("Ver Aviones");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        buttonGroup1.add(radioModelo);
        radioModelo.setSelected(true);
        radioModelo.setText("Modelo");

        buttonGroup1.add(radioMarca);
        radioMarca.setText("Marca");

        buttonGroup1.add(radioEstado);
        radioEstado.setText("Estado");

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/Icono32px.png"))); // NOI18N
        jLabel1.setText("Aviones");

        javax.swing.GroupLayout panelBotonesLayout = new javax.swing.GroupLayout(panelBotones);
        panelBotones.setLayout(panelBotonesLayout);
        panelBotonesLayout.setHorizontalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBotonesLayout.createSequentialGroup()
                .addGroup(panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelBotonesLayout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(radioEstado)
                                .addComponent(radioMarca)
                                .addComponent(radioModelo)
                                .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(textFieldBusqueda, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                                .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))))
                    .addGroup(panelBotonesLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)))
                .addContainerGap())
        );
        panelBotonesLayout.setVerticalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBotonesLayout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
                .addComponent(textFieldBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(radioModelo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(radioMarca)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(radioEstado)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelBotones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(panelTabla3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(60, 60, 60))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelBotones, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelTabla3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(38, 38, 38))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked
        setTabla("SELECT * FROM avion");
    }//GEN-LAST:event_jButton4MouseClicked

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        VentanaNuevoAvion nuevoAvion = new VentanaNuevoAvion(getMiConexion());
        nuevoAvion.setVisible(true);
        setTabla("SELECT * FROM avion");
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        int fila = jTable4.getSelectedRow();
        if (fila > -1){
            Avion modificarAvion = new Avion();
            //Trae el ID del avion seleccionado
            String id = jTable4.getValueAt(fila, 0).toString();
            String SQL = "SELECT * FROM avion WHERE id_avion = " + id;
            getMiConexion().sendSQLQuery(SQL);
            try {
                if (getMiConexion().getResultado().next()) {
                    modificarAvion.setId(Integer.parseInt(id));
                    modificarAvion.setEstado( Integer.parseInt(getMiConexion().getResultado().getObject("id_estado").toString()) );
                    Object marca = getMiConexion().getResultado().getObject("marca");
                    if (marca != null) modificarAvion.setMarca(marca.toString());
                    else modificarAvion.setMarca("");
                    Object modelo = getMiConexion().getResultado().getObject("modelo");
                    if (modelo != null) modificarAvion.setModelo(modelo.toString());
                    else modificarAvion.setModelo("");
                    modificarAvion.setNumeroAsientos(Integer.parseInt(getMiConexion().getResultado().getObject("numero_asientos").toString()));
                    Object serie = getMiConexion().getResultado().getObject("serie");
                    if (serie != null) modificarAvion.setSerieAvion(serie.toString());
                    else modificarAvion.setSerieAvion("");
                    Object ubicacion = getMiConexion().getResultado().getObject("ubicacion");
                    if (ubicacion != null) modificarAvion.setUbicacion(ubicacion.toString());
                    else modificarAvion.setUbicacion("");
                    VentanaNuevoAvion modificarUnAvion = new VentanaNuevoAvion(modificarAvion, getMiConexion());
                    modificarUnAvion.setVisible(true);
                    setTabla("SELECT * FROM avion");
                }
            } catch (SQLException ex) {
                Logger.getLogger(PanelAdministrarAviones.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        //Algoritmo para las busquedas
        String SQL;
        if (radioModelo.isSelected())
            SQL = "SELECT * FROM avion WHERE modelo ='" + textFieldBusqueda.getText() + "'";
        else if (radioMarca.isSelected())
            SQL = "SELECT * FROM avion WHERE marca ='" + textFieldBusqueda.getText() + "'";
        else
            SQL = "SELECT * FROM avion WHERE id_estado ='" + textFieldBusqueda.getText() + "'";
        setTabla(SQL);
    }//GEN-LAST:event_jButton3MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTable4;
    private javax.swing.JPanel panelBotones;
    private javax.swing.JPanel panelTabla3;
    private javax.swing.JRadioButton radioEstado;
    private javax.swing.JRadioButton radioMarca;
    private javax.swing.JRadioButton radioModelo;
    private javax.swing.JTextField textFieldBusqueda;
    // End of variables declaration//GEN-END:variables

}
